package com.skcc.cip.ngms.mail.processor;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.net.URLEncoder;

import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.commons.httpclient.Credentials;
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NTCredentials;
import org.apache.commons.httpclient.auth.AuthScheme;
import org.apache.commons.httpclient.auth.CredentialsNotAvailableException;
import org.apache.commons.httpclient.auth.CredentialsProvider;
import org.apache.commons.httpclient.methods.GetMethod;

public class NTLMSecurityProcessor implements Processor{

	private static HttpClient httpclient = new HttpClient();
	
	@Override
	public void process(Exchange exchange) throws Exception {
		sendPost((String) exchange.getIn().getHeader("authUser"), (String) exchange.getIn().getHeader("authPassword"), exchange);
	}
	
	private org.apache.commons.httpclient.params.HttpMethodParams setupCredential(String user, String password) {
		final NTCredentials nt = new NTCredentials(user, password, "hpwmail.skcc.com", "skcc");
		final CredentialsProvider myCredentialsProvider = new CredentialsProvider() {
			public Credentials getCredentials(AuthScheme scheme, String host, int port, boolean proxy) throws CredentialsNotAvailableException {
				return nt;
			}
		};
		org.apache.commons.httpclient.params.HttpMethodParams param = new org.apache.commons.httpclient.params.HttpMethodParams();
		param.setParameter("http.authentication.credential-provider",
				myCredentialsProvider);
		return param;
	}
	   
	public void sendPost(String id, String pw, Exchange exchange) throws Exception {
		httpclient.getParams().setAuthenticationPreemptive(false);

		// TODO 인코딩 제거해야 될 수 잇음
		String attachUrl = (String) exchange.getIn().getHeader("filepath")
				+ "&id=" + URLEncoder.encode((String) exchange.getIn().getHeader("id"), "UTF-8")
				+ "&attid0=" + URLEncoder.encode((String) exchange.getIn().getHeader("attid0"), "UTF-8")
				+ "&attach=" + (String) exchange.getIn().getHeader("attach");
		GetMethod get = new GetMethod(attachUrl);
		get.setParams(setupCredential(id, pw));
		BufferedInputStream stream = null;
		try {
			httpclient.executeMethod(get);
			Header[] responseHeaders = get.getResponseHeaders();
			exchange.getIn().setHeader("Content-Type", "application/octet-stream");
			for (Header header : responseHeaders) {
				exchange.getIn().setHeader(header.getName(), header.getValue());
			}

			stream = new BufferedInputStream(get.getResponseBodyAsStream());
			exchange.getIn().setBody(stream);
		} catch (Exception e) {
			// TODO 익셉션 처리~
		} finally {
			// Release current connection to the connection pool once you are
			// done
//			if( stream != null )
//				try { stream.close(); } catch (IOException ignore) {}
//			if( get != null ) get.releaseConnection();
//			get.releaseConnection();
		}
	}

}
